<?php
/**
 * 
 * @author tangbl
 *
 */
class CategoryModel extends Model {
	
	/*array(
		0.字段名   
		1.验证规则 [require 字段必须、email 邮箱、url URL地址、currency 货币、number 数字]
		2.提示信息
		3.验证条件 [0.(默认)存在字段就验证;1.必须验证;2.值不为空的时候验证]
		4.附加规则 [regex、function、callback、confirm、equal、in、length、between、expire、ip_allow、ip_deny、unique]
		5.验证时   [1.新增数据时候验证;2.编辑数据时候验证;3.(默认)全部情况下验证]
	);*/
	protected $_validate    =   array(
		array('lang','require','{$cat_form_validata_lang_required}',1,'',1),
		//array('module','require','{$cat_form_validata_module_required}',1,'',3),
		array('title','require','{$cat_form_validata_title_required}',1,'',3),
	);
	
	// 定义自动完成
	protected $_auto    =   array(
		array('addtime','time',1,'function'),//add
		
	);
	
	//得到对应语言包，对应模块的全局栏目
	public function getCateSelect($lang,$module='',$nid=0){
		/* $cates   = $this->getCateByModule($lang,$module);
		$row     = $this->getCateRoot($lang,$module);
		$selects = get_cate_select($cates,$row['pid'],$nid); */
		$cates   = $this->getCateByModule($lang);
		$pid     = 0;
		$selects = get_select_module($cates,$pid,$nid,$module);
		return $selects;
	}
	
	//得到对应语言包全局栏目
	public function getAllCate($lang,$nid=0){
		$pid     = 0;
		$cates   = $this->getCateByModule($lang);
		$selects = get_cate_select($cates,$pid,$nid);
		return $selects;
	}
		
	public function getCateByModule($lang,$module='') {
		$cates = F('cate_'.$lang.$module);
		if(empty($cates)){
			$where = "lang='".$lang."'";
			if($module){
				$where .= " and module='".$module."'";
			}
			$order = "sort desc";
			$rows  = $this->where($where)->order($order)->select();
			$cates = array();
			foreach($rows as $v){
				$cates[$v['id']] = $v;
			}
			F('cate_'.$lang.$module,$cates);
		}
		return $cates;
	}
	
	
	public function getCateRoot($lang,$module='') {
		return 0;
	}
	
	public function getCateTitle($id=0){
		if($id){
			$where = "id=".$id;
			$title  = $this->where("id=$id")->getField("title");
		}else{
			$title = L("cat_form_ele_top");
		}
		return $title;
	}	
	
	//更新是否有子分类字段
	public function updHaveLeaf($id,$flag=false){
		$haveLeaf          = 1;
		if($flag){
			$num = $this->where("pid = $id")->count();
			if($num == 0)$haveLeaf = 2 ;
		}
		$data['id']        = $id;
		$data['haveLeaf']  = $haveLeaf;
		$this->data($data)->save();
	}
	
	//生成分类 level path字段
	public function genCatePath($data,$id){
		$prefix = "-";
		$data['id'] = $id;
		if($data['pid']){
			$this->updHaveLeaf($data['pid']);
			
			$cate = $this->find($data['pid']);
			$data['level'] = intval($cate['level'])+1;
			$data['pids']  = $cate['pids'].$id.$prefix;
			$path          = $this->fillZeroLeft($data['pid']);
			if($data['level']>2){
				$path = $cate['path'].$path;
			}
			$data['path']  = $path;	
		}else{
			$data['sort']  = 1 ;
			$data['level'] = 1 ;
			$data['pids']  = $prefix.$id.$prefix;
			$data['path']  = $this->fillZeroLeft($id);
		}
		$data['url']   = $data['url'].$id;
		return $data;
	}

	public function fillZeroLeft($str,$maxLen=6){
		return str_pad($str,$maxLen,'0',STR_PAD_LEFT);	
	}
	
	
	//清除分类缓存
	function clean_cate_cache($lang,$module=''){
		
		F('cate_'.$lang,NULL);
		$this->getCateByModule($lang);
		if($module){
			F('cate_'.$lang.$module,NULL);
			$this->getCateByModule($lang,$module);
		}else{
			$all = C("LANG_CATEGORY");
			foreach($all as $k=>$v){
				F('cate_'.$lang.$k,NULL);
				$this->getCateByModule($lang,$k);
			}
		}
	}
	
	//得到分类模块名称
	function get_cate_module($key){
		if($key){
			
			$array   = F("Module_cate");
			if(array_key_exists($key, $array)){
				$v = $array[$key];
				$title = dejsonByLang($v[1]);
			}else{
				$module = C('LANG_CATEGORY');
				$title  = L($module[$key][1]);
			}
			return $title;
		}else{
			return "";
		}
	}
	
}
?>